/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package StartUp;

/**
 *
 * @author Mih
 */
import javax.swing.JFrame;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Rectangle;
import java.awt.TextArea;
import javax.swing.JButton;
import bluej.extensions.BClass;
import bluej.extensions.BField;
import bluej.extensions.BMethod;
import bluej.extensions.BPackage;
import bluej.extensions.BProject;
import bluej.extensions.ClassNotFoundException;
import bluej.extensions.PackageNotFoundException;
import bluej.extensions.ProjectNotOpenException;

public class ProjectInfo extends JFrame {

    private static final long serialVersionUID = 1L;
    private Panel panel = null;
    private TextArea textAreaInfo = null;
    private JButton buttonOk = null;
    private BProject bpkg;

    /**
     * This is the default constructor
     */
    public ProjectInfo(BProject b) {
        super();
        this.bpkg = b;
        initialize();
        getInfo();
    }

    private void getInfo() {
        String info = "";

        try {
            for (BPackage p : bpkg.getPackages()) {
                for (BClass c : p.getClasses()) {

                    if (c.getName().contains(".")) {
                        String name = "";
                        for (int i = c.getName().length() - 1; i >= 0; i--) {
                            if (c.getName().charAt(i) == '.') {
                                break;
                            } else {
                                name += c.getName().charAt(i);
                            }
                        }
                        String aux = "";
                        for (int i = name.length() - 1; i >= 0; i--) {
                            aux += name.charAt(i);
                        }
                        info += "Class Name: " + aux + "\n";
                    } else {
                        info += "Class Name: " + c.getName() + "\n";
                    }

                    if ("".equals(c.getPackage().getName())) {
                        info += "Package Class: Default\n\n";
                    } else {
                        info += "Package Class: " + c.getPackage().getName() + "\n\n";
                    }

                    if (c.getFields().length > 0) {
                        info += "   Fields:\n";
                        for (BField f : c.getFields()) {
                            info += "   -" + f.getName() + "\n";
                        }
                    }
                    if (c.getDeclaredMethods().length > 0) {
                        info += "\n   Declared Methods:\n";
                        for (BMethod b : c.getDeclaredMethods()) {
                            info += "   -" + b.getName() + "\n";
                        }
                        info += "___________________________________________________\n\n";
                    }
                }
                textAreaInfo.setText(info);
                textAreaInfo.setCaretPosition(0);
            }
        } catch (ProjectNotOpenException e) {
            // TODO Auto-generated catch block
        } catch (PackageNotFoundException e) {
            // TODO Auto-generated catch block
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
        }
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        this.setSize(400, 350);
        this.setTitle("Project Info");
        this.setLayout(null);
        this.setContentPane(getPanel());
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        this.setVisible(true);
    }

    /**
     * This method initializes panel	
     * 	
     * @return java.awt.Panel	
     */
    private Panel getPanel() {
        if (panel == null) {
            panel = new Panel();
            panel.setLayout(null);
            panel.add(getTextAreaInfo(), null);
            panel.add(getButtonOk(), null);
        }
        return panel;
    }

    /**
     * This method initializes textAreaInfo	
     * 	
     * @return java.awt.TextArea	
     */
    private TextArea getTextAreaInfo() {
        if (textAreaInfo == null) {
            textAreaInfo = new TextArea();
            textAreaInfo.setBounds(new Rectangle(3, 2, 380, 249));
            textAreaInfo.setBackground(this.getBackground());
            textAreaInfo.setEditable(false);
        }
        return textAreaInfo;
    }

    /**
     * This method initializes buttonOk	
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getButtonOk() {
        if (buttonOk == null) {
            buttonOk = new JButton();
            buttonOk.setBounds(new Rectangle(170, 265, 60, 30));
            buttonOk.setText("Ok");
            buttonOk.addActionListener(new Events());
        }
        return buttonOk;
    }

    class Events implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            // TODO Auto-generated method stub
            dispose();
        }
    }
}
